﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ISODOC.Models;
using WinNemoLib;

namespace ISODOC.Controllers
{
    public class MasterController : Controller
    {
        //
        // GET: /Master/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult DOC_TYPE()
        {
            return View();
        }

        public ActionResult OPT_TYPE()
        {
            return View();
        }

        #region "Doctype"        

        [HttpPost]
        public JsonResult DOC_TYPE_List()
        {
            string page = Request["page"];
            string perpage = Request["rp"];
            string qtype = Request.Form["qtype"].ToString();
            string sortBy = Request.Form["sortname"].ToString();
            string sortorder = Request.Form["sortorder"].ToString();

            int start = (Convert.ToInt32(page) - 1) * Convert.ToInt32(perpage);

            MasterModel rs = new MasterModel();
            List<MasterModel.Mast_DS> list = rs.SEL_DOC_MASTER("");

            if (sortorder == "asc")
            {
                list = list.OrderBy(r => r.GetType().GetProperty(sortBy).GetValue(r, null)).ToList();
            }
            if (sortorder == "desc")
            {
                list = list.OrderByDescending(r => r.GetType().GetProperty(sortBy).GetValue(r, null)).ToList();
            }
            var wOrder = from c in list select new { c._Code, c._Type };
            List<MasterModel.Mast_DS> list2 = list.Skip(Convert.ToInt32(page) - 1 * Convert.ToInt32(perpage)).Take(Convert.ToInt32(perpage)).ToList();

            Flexigrid.FlexigridMas_List flexigridObject = new Flexigrid.FlexigridMas_List();
            flexigridObject.page = 1;
            flexigridObject.total = list.Count();
            flexigridObject.rows = list2;
            return Json(flexigridObject);
        }

        [HttpPost]
        public JsonResult ListDOC_TYPE(string code = "")
        {
            MasterModel rs = new MasterModel();
            List<MasterModel.Mast_DS> list = rs.SEL_DOC_MASTER(code);
            return Json(list);
        }

        [HttpPost]
        public JsonResult add_doctype(List<MasterModel.Mast_DS> theData)
        {
            MasterModel.Mast_DS data = theData[0];
            MasterModel ins = new MasterModel();

            List<MasterModel.Mast_DS> list = ins.SEL_DOC_MASTER(data._Code.ToString());
            if (list.Count > 0)
            {
                return Json(new { success = 0, message = "DATA EXIT" });
            }
            using (ConnectionWrapper cnn = WinNemoLib.Nemo.GetDefaultConfiguredDbConnectionWrapper())
            {
                cnn.Open();
                cnn.BeginTransaction();
       

                if (ins.Ins_DocType(cnn, data) == false)
                {
                    return Json(new { success = 0, message = "CANNOT INSERT" });
                }
                cnn.CommitTransaction();

            }

            return Json(new { success = 1, message = "INSERT COMPELTE" });
        }
        [HttpPost]
        public JsonResult Edit_doctype(List<MasterModel.Mast_DS> theData)
        {
            MasterModel.Mast_DS data = theData[0];
            using (ConnectionWrapper cnn = WinNemoLib.Nemo.GetDefaultConfiguredDbConnectionWrapper())
            {
                cnn.Open();
                cnn.BeginTransaction();
           
                MasterModel ins = new MasterModel();
                if (ins.Edit_DocType(cnn, data) == false)
                {
                    return Json(new { success = 0, message = "CANNOT EDIT" });
                }
                cnn.CommitTransaction();

            }

            return Json(new { success = 1, message = "EDIT COMPELTE" });
        }

        #endregion

        #region "opttype"

        [HttpPost]
        public JsonResult OPT_TYPE_List()
        {
            string page = Request["page"];
            string perpage = Request["rp"];
            string qtype = Request.Form["qtype"].ToString();
            string sortBy = Request.Form["sortname"].ToString();
            string sortorder = Request.Form["sortorder"].ToString();

            int start = (Convert.ToInt32(page) - 1) * Convert.ToInt32(perpage);

            MasterModel rs = new MasterModel();
            List<MasterModel.Mast_DS> list = rs.SEL_OPT_MASTER("");

            if (sortorder == "asc")
            {
                list = list.OrderBy(r => r.GetType().GetProperty(sortBy).GetValue(r, null)).ToList();
            }
            if (sortorder == "desc")
            {
                list = list.OrderByDescending(r => r.GetType().GetProperty(sortBy).GetValue(r, null)).ToList();
            }
            var wOrder = from c in list select new { c._Code, c._Type };
            List<MasterModel.Mast_DS> list2 = list.Skip(Convert.ToInt32(page) - 1 * Convert.ToInt32(perpage)).Take(Convert.ToInt32(perpage)).ToList();

            Flexigrid.FlexigridMas_List flexigridObject = new Flexigrid.FlexigridMas_List();
            flexigridObject.page = 1;
            flexigridObject.total = list.Count();
            flexigridObject.rows = list2;
            return Json(flexigridObject);
        }

        [HttpPost]
        public JsonResult LISTOPT_TYPE(string code="")
        {
            MasterModel rs = new MasterModel();
            List<MasterModel.Mast_DS> list = rs.SEL_OPT_MASTER(code);
            return Json(list);
        }

        [HttpPost]
        public JsonResult add_opttype(List<MasterModel.Mast_DS> theData)
        {
            MasterModel.Mast_DS data = theData[0];
            MasterModel ins = new MasterModel();

            List<MasterModel.Mast_DS> list = ins.SEL_OPT_MASTER(data._Code.ToString());
            if (list.Count > 0)
            {
                return Json(new { success = 0, message = "DATA EXIT" });
            }
            using (ConnectionWrapper cnn = WinNemoLib.Nemo.GetDefaultConfiguredDbConnectionWrapper())
            {
                cnn.Open();
                cnn.BeginTransaction();


                if (ins.Ins_OptType(cnn, data) == false)
                {
                    return Json(new { success = 0, message = "CANNOT INSERT" });
                }
                cnn.CommitTransaction();

            }

            return Json(new { success = 1, message = "INSERT COMPELTE" });
        }
        [HttpPost]
        public JsonResult Edit_opttype(List<MasterModel.Mast_DS> theData)
        {
            MasterModel.Mast_DS data = theData[0];
            using (ConnectionWrapper cnn = WinNemoLib.Nemo.GetDefaultConfiguredDbConnectionWrapper())
            {
                cnn.Open();
                cnn.BeginTransaction();

                MasterModel ins = new MasterModel();
                if (ins.Edit_OptType(cnn, data) == false)
                {
                    return Json(new { success = 0, message = "CANNOT EDIT" });
                }
                cnn.CommitTransaction();

            }

            return Json(new { success = 1, message = "EDIT COMPELTE" });
        }

        #endregion
    }
}
